<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<cus-search @seachHandler="toSearch"></cus-search>
		</view>
		<!-- 轮播图 -->
			<swiper class="swiper" circular="true" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item v-for="(item,index) in swiperList" :key="index">
					<navigator :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id" class="swiper-item">
						<image :src="item.image_src"></image>
					</navigator>
				</swiper-item>
			</swiper>
			
			<!-- 分类导航 -->
			<view class="nav-list">
				<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="toCate(item)">
					<image :src="item.image_src"></image>
				</view>
			</view>
			
			<!-- 楼层区域 -->
			<view class="floor-list">
				<view class="floor-item" v-for="(item, index) in floorList" :key="index">
					<!-- 标题 -->
					<image :src="item.floor_title.image_src" mode="widthFix"></image>
					<!-- 图片区域 -->
					<view class="floor-img-box">
						<navigator class="left-img-box" :url="item.product_list[0].url">
							<image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
						</navigator>
						<view class="right-img-box">
							<navigator class="right-img-item" v-for="(subitem, i) in item.product_list" :key="i" v-if="i !== 0" :url="subitem.url">
								<image :src="subitem.image_src" :style="{width: subitem.image_width + 'rpx'}" mode="widthFix" ></image>
							</navigator>
						</view>
					</view>
				</view>
			</view>
	</view>
</template>

<script>
	import mixBadge from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [mixBadge],
		data() {
			return {
				// 轮播图数据
				swiperList: [],
				// 分类导航
				navList: [],
				// 楼层导航
				floorList: []
			}
		},
		methods: {
			// 获取轮播图数据
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// console.log(res);
				// 失败
				if (res.meta.status !== 200) return uni.$showMsg()
				// 成功
				this.swiperList = res.message
				// uni.$showMsg("数据加载成功")
			},
			
			// 获取分类导航数据
			async getNavList() {
				const {data: res} = await uni.$http.get('/api/public/v1/home/catitems')
				// console.log(res);
				if(res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},
			
			// 分类导航点击事件处理函数
			toCate(item) {
				// 判断是否点击的是分类
				if(item.name === '分类') {
					uni.switchTab({
						url:'/pages/cate/cate'
					})
				}
			},
			
			// 获取楼层导航数据
			async getFloorList() {
				const {data: res} = await uni.$http.get('/api/public/v1/home/floordata')
				// console.log(res);
				if(res.meta.status !== 200) return uni.$showMsg()
				// 返回的数据地址与本地不一样，通过循环,处理url地址
				res.message.forEach(floor => {
					floor.product_list.forEach(pro => {
					// 	// 将本地的url地址挂载到product_list里面
						pro.url = '/subpkg/goods_list/goods_list?' + pro.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},
			
			// 跳转至搜索页面
			toSearch() {
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		}
	}
</script>
	
<style lang="scss">
	// 设置搜索框吸顶效果
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.swiper {
		height: 330rpx;
		.swiper-item, image {
			width: 100%;
			height: 100%;
		}
	}
	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15rpx 0;
		.nav-item {
			image {
				width: 128rpx;
				height: 140rpx;
			}
		}
	}
	.floor-img-box {
		display: flex;
		padding-left: 10rpx;
		.right-img-box {
			display: flex;
			flex-flow: wrap;
			justify-content: space-around;
		}
	}
	
</style>
